package com.chat.demo.userservice.Entity.DAO;

import com.chat.demo.userservice.Entity.Entity.Message;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageMapper {

    // 发送消息
    @Insert("INSERT INTO app.message(sender_id, receiver_type, receiver_id, content, status) " +
            "VALUES(#{senderId}, 'room', #{roomId}, #{content}, 'sent')")
    int sendRoomMessage(@Param("senderId") int senderId,
                        @Param("roomId") int roomId,
                        @Param("content") String content);

    // 查询群聊历史消息（分页）
    @Select("SELECT * FROM app.message " +
            "WHERE receiver_type = 'room' AND receiver_id = #{roomId} " +
            "ORDER BY sent_at ASC " +
            "LIMIT #{limit} OFFSET #{offset}")
    List<Message> getRoomMessages(@Param("roomId") int roomId,
                                  @Param("limit") int limit,
                                  @Param("offset") int offset);

    // 更新消息状态（群聊一般是广播，不一定每人一条记录）
    @Update("UPDATE app.message SET status = #{status} WHERE message_id = #{messageId}")
    int updateMessageStatus(@Param("messageId") int messageId,
                            @Param("status") String status);

    // 获取所有群组成员（用于广播）
    @Select("SELECT user_id FROM app.user_chat_room WHERE room_id = #{roomId}")
    List<Integer> getRoomMembers(@Param("roomId") int roomId);
}
